package me.leiusual.sharing.ui.account

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.UserInfo
import me.leiusual.sharing.pref.User
import me.leiusual.sharing.repository.UserRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.ui.account.model.ParamsAccessToken
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * Create by leiusual on 2018/7/16
 */
class AccountLoginViewModel @Inject constructor(
    private val userRepository: UserRepository,
    val user: User
) : ViewModel() {

  private val _accessToken = MutableLiveData<ParamsAccessToken>()
  val accessToken: LiveData<Data<UserInfo.Response>> = Transformations.switchMap(_accessToken) {
    userRepository.accessToken(it).toLiveData()
  }

  fun accessToken(username: String, password: String) {
    _accessToken.value = ParamsAccessToken(username, password)
  }
}